<template>
	<div class="twoStageWindow" style="padding-bottom:10px;">
		<!--{{getPlanData}}-->
		<div class="twoStageWindowTitle">
			{{title}}
			<div class="twoStageWindowClose">
				<el-button size="mini" type="primary" @click="saveInfo(1)" v-if="showButton.submitButton" :disabled="!isRepeat">提交</el-button>
				<el-button size="mini" type="primary" @click="saveInfo(0)" v-if="showButton.saveButton" :disabled="!isRepeat">保存</el-button>

				<el-button size="small" type="primary" @click="auditTask" v-if="showButton.auditButton  && $parent.userOpernation.issue.display" :disabled="!isRepeat">签发</el-button>
				<el-button size="small" type="primary" @click="checkTask" v-if="showButton.checkButton  && $parent.userOpernation.check.display" :disabled="!isRepeat">审核</el-button>

				<el-button size="mini" v-on:click="close">{{ title=='查看模板'?'关闭':'取消' }}</el-button>
			</div>
		</div>
		<div class="containercontent" style="margin-top:40px;height:calc(100% - 40px);overflow:auto;">
			<div style="margin:10px auto;width:calc(100% - 20px);" class="tabCard">

				<div v-for="item in dealTabArr" :class="{'tabName':true,'activeTab':nowTab==item.path}" @click="changeTab(item.path)" :key="item.path">
					{{item.name}}
				</div>
			</div>
			<component v-bind.sync="$props" ref="child" v-bind:is="nowTab" class="tab" style="padding:0 20px;height:calc(100% - 70px)"></component>
		</div>


		<audit-task-dialog :auditTaskDialogShow.sync="auditTaskDialogShow" :getPlanData.sync="getPlanData"></audit-task-dialog>
		<check-task-dialog :auditTaskDialogShow.sync="checkTaskDialogShow" :getPlanData.sync="getPlanData"></check-task-dialog>

	</div>
</template>

<script>
	import { mapState } from 'vuex'
	import basicInfo from './component/basicInfo.vue' //基础信息
	import monitoringScheme from './component/monitoringscheme.vue' //检测方案

	import auditTaskDialog from './children/auditTaskDialog.vue'
	import checkTaskDialog from './children/checkTaskDialog.vue'
	const jiancejieguo = () =>
		import('../../../testmanage/resultexamine.vue')

	import caiyangxinxi from './component/caiyangxinxi.vue' //采样信息
	import shenghejilu from './component/shenghejilu.vue' //流程记录
	import renwufujian from './component/renwufujian.vue'



	export default {
		props: ['taskDetailShow', 'type', 'canNotEdit', 'showCycleDetail', 'isPlan', 'looksll', 'bidui', 'isBD', 'lookJieGuo', 'bigtype', 'ifnewtask', 'title', 'getPlanData', 'showTabIndex', 'showButton'],
		data() {
			const qcOptions = ['现场平行', '现场加标'];
			return {
				checkTaskDialogShow: false,
				auditTaskDialogShow: false,
				checkedQC: [], //选中的选项
				QCS: qcOptions,
				addPointShow: false,
				msg: '',
				nowTab: 'basicInfo',
				tabArr: [{
					path: 'basicInfo',
					name: '基础信息'
				},
				{
					path: 'monitoringScheme',
					name: '监测方案'
				},
				{
					path: 'jiancejieguo',
					name: '检测结果'
				},
				{
					path: 'caiyangxinxi',
					name: '样品信息'
				},
				{
					path: 'shenghejilu',
					name: '流程记录'
				},
				{
					path: 'renwufujian',
					name: '任务附件'
				},

				],
				taskTypeTableDate: [],
				popupShow: false,
				isRepeat: true,

			}
		},
		components: {
			basicInfo,
			monitoringScheme,
			jiancejieguo,
			caiyangxinxi,
			shenghejilu,
			renwufujian,
			auditTaskDialog,
			checkTaskDialog
		},
		computed: {
			...mapState(['cycleMode', 'cycStatus', 'inspectionUnitTableDate2', 'waibuorneibu', 'status', 'bottleType', 'moduleId']),
			dealTabArr() {

				let showTabs = [];
				for (let i of this.showTabIndex) {
					showTabs.push(this.tabArr[i])
				}
				return showTabs
			},
			pointTableHeight() {
				return (((window.innerHeight - 190) * 0.3) - 40)
			},
			pickSampleTaskHeight() {
				return (((window.innerHeight - 190) * 0.3) - 40)
			},
			testTableHeight() {
				return (((window.innerHeight - 190) * 0.4) - 40)
			},
			taskListTableHeight: function () {
				return (window.innerHeight - 360)
			},
		},
		watch: {
			taskTypeTableDateValue: function (val) {
				if (val == 'BD') {
					this.waibuorneibuValue = '001'
				}
			}
		},
		mounted() {
			this.$store.dispatch('getAllClients');

		},

		methods: {
			auditTask() {
				this.auditTaskDialogShow = true;
			},
			checkTask() {
				this.checkTaskDialogShow = true;
			},
			closeTask() {
				this.$parent.taskDetailShow = false;
				if (this.$parent.readTasks) {
					this.$parent.readTasks()
				}
				if (this.$parent.getPlanList) {
					this.$parent.getPlanList(this.$parent.planQuery)
				}

			},
			saveInfo(status) {
				console.log(this.getPlanData,this.title)
				this.nowTab = 'basicInfo';
				this.$nextTick(() => {
					this.$refs.child.$refs.basicInfoData.validate((valid) => {
						if (valid) {
							this.getPlanData.submitType = status;
							if (status == 1) {
								this.getPlanData.moduleId = this.moduleId;
								this.getPlanData.operation = "check";
							}
							if (this.title == '编辑模板') {
								if (this.getPlanData.doAuto == false) {
									this.getPlanData.timeExpression = null
								} else {
									if (typeof this.getPlanData.timeExpression.hours == "object") {
										this.getPlanData.timeExpression.hours = this.getPlanData.timeExpression.hours.join(',')
									}
									if (typeof this.getPlanData.timeExpression.day == "object") {
										this.getPlanData.timeExpression.day = this.getPlanData.timeExpression.day.join(',')
									}
									if (typeof this.getPlanData.timeExpression.month == "object") {
										this.getPlanData.timeExpression.month = this.getPlanData.timeExpression.month.join(',')
									}
									if (typeof this.getPlanData.timeExpression.week == "object") {
										this.getPlanData.timeExpression.week = this.getPlanData.timeExpression.week.join(',')
									}
								}

								this.httpMethod('/detection/updatePlan.htm', this.getPlanData).then((res) => {
									this.$parent.taskDetailShow = false;
									this.$parent.getPlanList(this.$parent.planQuery) //刷新页面

								})
							} else if (this.title == '添加模板'  || this.title == '模板复制') {
								if (this.getPlanData.doAuto == false) {
									this.getPlanData.timeExpression = null
								} else {
									if (typeof this.getPlanData.timeExpression.hours == "object") {
										this.getPlanData.timeExpression.hours = this.getPlanData.timeExpression.hours.join(',')
									}
									if (typeof this.getPlanData.timeExpression.day == "object") {
										this.getPlanData.timeExpression.day = this.getPlanData.timeExpression.day.join(',')
									}
									if (typeof this.getPlanData.timeExpression.month == "object") {
										this.getPlanData.timeExpression.month = this.getPlanData.timeExpression.month.join(',')
									}
									if (typeof this.getPlanData.timeExpression.week == "object") {
										this.getPlanData.timeExpression.week = this.getPlanData.timeExpression.week.join(',')
									}
								}

								// return;
								this.httpMethod('/detection/addPlan.htm', this.getPlanData).then((res) => {

									this.$parent.taskDetailShow = false;
									this.$parent.getPlanList(this.$parent.planQuery)
								})
							} else if (this.title == '任务创建' || this.title == '任务复制' || this.title == '模板任务' || this.title == '子任务创建') {
								this.getPlanData.commitNum = 1

								this.httpMethod('/detection/addTask.htm', this.getPlanData).then((res) => {
									this.$parent.taskDetailShow = false;
									this.$parent.readTasks()
								})

							} else if (this.title == '任务提交') {
								if (this.getPlanData.status == "502") {
									this.getPlanData.submitType = 2;
									this.$confirm('是否提交修改' , '提示', { //待检测状态的话提交要提示
										confirmButtonText: '确定',
										cancelButtonText: '取消',
										type: 'warning'
									}).then(() => {
										this.getPlanData.commitNum = this.getPlanData.commitNum + 1
										this.httpMethod('/detection/updateTask.htm', this.getPlanData).then((res) => {
											this.$parent.taskDetailShow = false;
											this.$parent.readTasks()
										})
									})
								}else{
									this.getPlanData.commitNum = this.getPlanData.commitNum + 1
									this.httpMethod('/detection/updateTask.htm', this.getPlanData).then((res) => {
										this.$parent.taskDetailShow = false;
										this.$parent.readTasks()
									})
								}

							}
						} else {
							this.$message({
								showClose: true,
								message: '请检查基础信息必填项！',
								type: 'warning'
							})
							return false;
						}
					});
				})
			},
			httpMethod(url, param) {
				this.isRepeat = false
				return new Promise((resolve, reject) => {
					this.$http({
						method: 'post',
						apiUrl: url,
						params: param,
						isJson: true,
            isLoading:true,
					}).then(res => {
						this.isRepeat = true;
						if (res.code === 0) {
							this.$notify({
								showClose: true,
								message: '操作成功！',
								type: 'success',
								title:"成功"
							})
							resolve(res)
						}
					})
				})
			},
			changeTab(tab) {
				this.nowTab = tab
			},
			newTask: function () {
				this.$parent.taskDetailShow = false;
			},
			close: function () {
				this.$parent.taskDetailShow = false;
				this.isRepeat = true;
			},

			resetPointForm() {
				this.addPointShow = false;
			},

		}
	}

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
	.tabCard .tabName {
		margin-right: 0;
	}

	.tabCard .activeTab {
		background: #fff;
	}
</style>
